<?php
/*
 * @author eBIZ Team <dev@jungo.com.cn>
 * @copyright  Copyright (c) , ShenZhen JunGO Technology Co., Ltd. All rights reserved.
 * @license  Commercial authorization, unauthorized use and modification are prohibited
 * @url www.jungo.com.cn
 */

namespace ckeeper\utils;


use ckeeper\utils\Ip;
use Selibra\Di\DI;
use Selibra\Http\Lib\Request;
use ckeeper\dao\ActionLogDao;
use ckeeper\entity\ActionLogEntity;

/**
 * 管理员操作记录器
 * Class ManagerActionRecorder
 * @package Admin\Account\Utils
 */
class ManagerActionRecorder
{

    /**
     * 记录普通消息日志
     * @param $uuid
     * @param $type
     * @param $content
     * @param $request
     * @param array $data
     * @throws \ReflectionException
     * @throws \Selibra\Di\Exception\NoImplementClass
     * @throws \Selibra\Model\Exception\DbException
     */
    public static function info($uuid, $type, $content, Request $request, array $data = [])
    {
        $actionLog = new ActionLogEntity();
        $actionLog->uuid = $uuid;
        $actionLog->type = $type;
        $actionLog->content = $content;
        $actionLog->ip = Ip::getRequestIp($request);
        $actionLog->url = $request->getPathInfo();
        if (!empty($data)) {
            $actionLog->data = json_encode($data);
        }
        $actionLog->time = time();
        $actionLog->create_time = time();
        self::actionLogDao()->record($actionLog);
    }


    /**
     * @return ActionLogDao
     * @throws \ReflectionException
     * @throws \Selibra\Di\Exception\NoImplementClass
     */
    private static function actionLogDao(): ActionLogDao
    {
        return DI::getObjectContext(ActionLogDao::class);
    }

}
